﻿using System;
using System.Collections.Generic;
using System.Linq;
using ArchPostprocessing.CompoundStructureManage.DataDefinition;
using ArchPostprocessing.CompoundStructureManage.Managers;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using RevitInteractiveUtility.ExternalEventManager;

namespace ArchPostprocessing.RoomDecorate
{
	internal class RoomDecorateFormRequest : IRequest
	{
		public RoomDecorateFormRequest(int iRequestId)
		{
			this.m_iRequestId = iRequestId;
		}

		public int RequestId
		{
			get
			{
				return this.m_iRequestId;
			}
		}

		public void DoRequest(object parameters = null)
		{
			RoomDecorateForm roomDecorateForm = parameters as RoomDecorateForm;
			ExternalCommandData cmdData = roomDecorateForm.m_cmdData;
			Document document = cmdData.Application.ActiveUIDocument.Document;
			for (;;)
			{
				List<Element> list = new List<Element>();
				try
				{
					list = cmdData.Application.ActiveUIDocument.Selection.PickElementsByRectangle(new RoomFilter(), "请选择房间：").ToList<Element>();
					if (list == null || list.Count == 0)
					{
						continue;
					}
					roomDecorateForm.m_lstSelRooms = list;
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					roomDecorateForm.Close();
					break;
				}
				catch (Exception ex)
				{
					YJKMessageBox.Show(ex.Message);
					roomDecorateForm.Close();
					break;
				}
				TransactionGroup transactionGroup = new TransactionGroup(document, "DecorateRoom");
				transactionGroup.Start();
				DrawDecorateEnt drawDecorateEnt = new DrawDecorateEnt(cmdData, roomDecorateForm.m_defInfoForm, list);
				try
				{
					Transaction transaction = new Transaction(document);
					transaction.Start("RoomDecorateDraw");
					cmdData.Application.Application.FailuresProcessing += this.OnFailuresProcessing;
					roomDecorateForm.SaveFormInfo();
					Autodesk.Revit.DB.ElementType selectElementTypeMetope = roomDecorateForm.GetSelectElementTypeMetope();
					Autodesk.Revit.DB.ElementType selectElementTypeGround = roomDecorateForm.GetSelectElementTypeGround();
					Autodesk.Revit.DB.ElementType selectElementTypeCeiling = roomDecorateForm.GetSelectElementTypeCeiling();
					Autodesk.Revit.DB.ElementType selectElementTypeSkirtline = roomDecorateForm.GetSelectElementTypeSkirtline();
					List<Floor> list2 = new List<Floor>();
					if (roomDecorateForm.m_defInfoForm.IncludeGround)
					{
						drawDecorateEnt.DrawRoomDecorateGrounds(ref list2, selectElementTypeGround);
					}
					if (roomDecorateForm.m_defInfoForm.IncludeCeiling)
					{
						List<Floor> list3 = new List<Floor>();
						drawDecorateEnt.DrawRoomDecorateCeilings(ref list3, selectElementTypeCeiling);
						if (list3.Count > 0)
						{
							list2.AddRange(list3);
						}
					}
					transaction.Commit();
					List<OffsetWallInfo> list4 = new List<OffsetWallInfo>();
					Transaction transaction2 = new Transaction(document);
					transaction2.Start("RoomDecorateDraw");
					if (roomDecorateForm.m_defInfoForm.IncludeMetope && drawDecorateEnt.DrawRoomDecorateMetopes(ref list4, selectElementTypeMetope) && roomDecorateForm.m_defInfoForm.IncludeSkirtline)
					{
						drawDecorateEnt.DrawRoomDecorateSkirtlines(selectElementTypeSkirtline, selectElementTypeMetope);
					}
					transaction2.Commit();
					if (list4.Count > 0)
					{
						bool bWithoutTransaction = false;
						WallCommon.JoinWalls(list4, document, bWithoutTransaction);
					}
					transactionGroup.Assimilate();
					continue;
				}
				catch (Exception ex2)
				{
					YJKMessageBox.Show(ex2.Message);
					roomDecorateForm.Close();
					transactionGroup.RollBack();
					continue;
				}
				break;
			}
		}

		private void OnFailuresProcessing(object sender, FailuresProcessingEventArgs e)
		{
			e.GetFailuresAccessor().DeleteAllWarnings();
		}

		private int m_iRequestId;
	}
}
